package com.crm.service.repositoryService;

import com.crm.dao.productionDAO.ProductionDetilsDAO;
import com.crm.dao.purchaseDAO.PurchaseDAO;
import com.crm.dao.purchaseDAO.PurchaseproductiondetailDAO;
import com.crm.dao.purchaseDAO.PurchasergDAO;
import com.crm.dao.saling.DispatcherDao;
import com.crm.dao.saling.DispatcherDetailsDao;
import com.crm.dao.saling.RefundDetailsDao;
import com.crm.pojo.*;
import com.crm.vo.DispacherVO;
import com.crm.vo.IOdetailVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class IOdetailsService {
    @Autowired
    private PurchaseDAO purchaseDao;
    @Autowired
    private DispatcherDao dispatcherDao;
    @Autowired
    private DispatcherDetailsDao dispatcherDetailsDao;
    @Autowired
    private PurchaseproductiondetailDAO purchaseproductiondetailDAO;
    @Autowired
    private ProductionDetilsDAO productionDetilsDAO;
    @Autowired
    private RefundDetailsDao refundDetailsDao;
    @Autowired
    private PurchasergDAO purchasergDAO;

    //通过ioid查找对应的采购单
    public Purchase findOneP(Long ioid){
        return purchaseDao.findOne(ioid);
    }
    //通过ioid查找对应的发货单
    public Dispatcher findOneD(Long ioid){
        return dispatcherDao.findOne(ioid);
    }
    //通过采购purid查找采购明细集合(包含部分产品信息)
    public List<IOdetailVO> findAllIOdetailP(Long purid){
        List<IOdetailVO> iolist = new ArrayList<>();
        List<Purchaseproductiondetail> plist = purchaseproductiondetailDAO.findAllByPid(purid);
        for (Purchaseproductiondetail p :plist) {
            ProductionDetail pd = productionDetilsDAO.findOne(p.getProid());
            IOdetailVO io = new IOdetailVO();
            io.setIodId(p.getPpdid());
            io.setIoacount(p.getPpcount());
            io.setIodpName(pd.getPname());
            io.setIodpImg(pd.getPimg());
            iolist.add(io);
        }
        return iolist;
    }
    //通过采购purid获取采购明细数量
    public Long queryCountP(Long purid){
        return purchaseproductiondetailDAO.queryCountP(purid);
    }
    //通过发货单id获取发货明细数量
    public Long queryCountD(Long did){
        return dispatcherDetailsDao.queryCountD(did);
    }
    //通过采购退货id获取采购退货明细数量
    public Long queryCountOut(Long id){
        return 1L;
    }
    //通过退货单id获取订单退货明细数量
    public Long queryCountIn(Long id){
        return refundDetailsDao.queryCountIn(id);
    }
    //通过退货单id获取订单退货明细
    public List<IOdetailVO> findAllIOdetailIn(Long id){
        List<IOdetailVO> iolist = new ArrayList<>();
        List<RefundDetail> rlist = refundDetailsDao.queryAllByid(id);
        for (RefundDetail refundDetail:rlist) {
            ProductionDetail p = productionDetilsDAO.findOne(refundDetail.getFkRefundProductId().longValue());
            IOdetailVO io = new IOdetailVO();
            io.setIodpImg(p.getPimg());
            io.setIodpName(p.getPname());
            io.setIoacount(refundDetail.getRefundDetailsId());
            io.setIoacount(refundDetail.getRefundDetailsAmount().longValue());
            iolist.add(io);
        }
        return iolist;
    }
    //通过发货单did查找发货明细集合(包含部分产品信息)
    public List<IOdetailVO> findAllIOdetailD(Long did){
        List<IOdetailVO> iolist = new ArrayList<>();
        List<DispatcherDetail> dlist = dispatcherDetailsDao.findAllBydid(did);
        for (DispatcherDetail d :dlist) {
            ProductionDetail pd = productionDetilsDAO.findOne(d.getPId().longValue());
            IOdetailVO io = new IOdetailVO();
            io.setIodId(d.getDispatcherDetailsId());
            io.setIoacount(d.getProductAccount().longValue());
            io.setIodpName(pd.getPname());
            io.setIodpImg(pd.getPimg());
            iolist.add(io);
        }
        return iolist;
    }
    //通过采购退货id获取采购退货明细
    public List<IOdetailVO> findAllIOdetailOut(Long id){
        List<IOdetailVO> iolist = new ArrayList<>();
        Purchasergdetail purchasergdetail = purchasergDAO.findOne(id);
        ProductionDetail p = productionDetilsDAO.findOne(purchaseproductiondetailDAO.findOne(purchasergdetail.getPpurdid()).getProid());
        IOdetailVO io = new IOdetailVO();
        io.setIodpName(p.getPname());
        io.setIodpImg(p.getPimg());
        io.setIodId(purchasergdetail.getPpurdid());
        io.setIoacount(purchasergdetail.getPrgcount());
        iolist.add(io);
        return iolist;
    }
}
